package com.minimalist.lindi.controller;

import com.minimalist.common.config.sse.SseManagerService;
import com.minimalist.common.response.R;
import com.minimalist.lindi.manager.DashboardManager;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.List;
import java.util.Map;

/**
 * 林地指标交易平台数据可视化控制器
 * 提供SSE实时数据推送功能
 *
 * @author 11987
 */
@Validated
@RestController
@Tag(name = "林地指标交易平台_数据可视化")
@RequestMapping("/lindi/dashboard")
public class LindiDataviewController{

    private static final Logger log = LoggerFactory.getLogger(LindiDataviewController.class);

    /**
     * SSE管理服务
     */
    @Autowired
    private SseManagerService sseManagerService;

    /**
     * 仪表板数据管理器
     */
    @Autowired
    private DashboardManager dashboardManager;

    /**
     * 建立SSE连接，用于实时推送数据
     */
    @GetMapping(value = "/sse/connect", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @Operation(summary = "建立SSE连接")
    public SseEmitter connect(@RequestParam(value = "clientId", required = false) String clientId){
        // 使用SSE管理服务创建连接

        return sseManagerService.createConnection(clientId);
    }

    /**
     * 启动数据推送定时任务
     */
    @PostMapping("/sse/start-push")
    @Operation(summary = "启动数据推送")
    public R<?> startDataPush(){
        // 使用SSE管理服务启动定时推送任务
        sseManagerService.startDataPush(this::generateDashboardData);
        return R.ok("数据推送已启动");
    }

    /**
     * 停止数据推送
     */
    @PostMapping("/sse/stop-push")
    @Operation(summary = "停止数据推送")
    public R<?> stopDataPush(){
        sseManagerService.stopDataPush();
        return R.ok("数据推送已停止");
    }

    /**
     * 获取当前连接数
     */
    @GetMapping("/sse/connections")
    @Operation(summary = "获取当前SSE连接数")
    public R<Map<String, Object>> getConnections(){
        return R.ok(sseManagerService.getConnectionInfo());
    }

    /**
     * 手动触发数据推送
     */
    @PostMapping("/sse/push-now")
    @Operation(summary = "手动触发数据推送")
    public R<?> pushDataNow(){
        Map<String, Object> dashboardData = generateDashboardData();
        sseManagerService.pushDataNow(dashboardData);
        return R.ok("数据推送完成");
    }

    /**
     * 生成仪表板数据
     * 从数据库获取真实数据
     */
    private Map<String, Object> generateDashboardData(){
        // 使用DashboardManager获取真实数据
        return dashboardManager.generateDashboardData();
    }


    /**
     * 格式化数字
     */
    private String formatNumber(double number){
        return String.format("%.1f", number);
    }

    /**
     * 获取平台统计概览（HTTP接口）
     */
    @GetMapping("/overview")
    @Operation(summary = "获取平台统计概览")
    public R<Map<String, Object>> getOverview(){
        return R.ok(dashboardManager.getPlatformOverview());
    }

    /**
     * 获取各街道镇区林地面积数据（HTTP接口）
     */
    @GetMapping("/area-statistics")
    @Operation(summary = "获取各街道镇区林地面积数据")
    public R<List<Map<String, Object>>> getAreaStatistics(){
        return R.ok(dashboardManager.getAreaStatistics());
    }

    /**
     * 获取各镇区街道林地交易情况（HTTP接口）
     */
    @GetMapping("/trade-statistics")
    @Operation(summary = "获取各镇区街道林地交易情况")
    public R<List<Map<String, Object>>> getTradeStatistics(){
        return R.ok(dashboardManager.getTradeStatistics());
    }

    /**
     * 获取资金营收统计（HTTP接口）
     */
    @GetMapping("/revenue-statistics")
    @Operation(summary = "获取资金营收统计")
    public R<List<Map<String, Object>>> getRevenueStatistics(){
        return R.ok(dashboardManager.getRevenueStatistics());
    }

    /**
     * 获取地图数据（HTTP接口）
     */
    @GetMapping("/map-data")
    @Operation(summary = "获取地图数据")
    public R<List<Map<String, Object>>> getMapData(){
        return R.ok(dashboardManager.getMapData());
    }

    /**
     * 获取林地补贴申请统计（HTTP接口）
     */
    @GetMapping("/subsidy-statistics")
    @Operation(summary = "获取林地补贴申请统计")
    public R<List<Map<String, Object>>> getSubsidyStatistics(){
        return R.ok(dashboardManager.getSubsidyStatistics());
    }

    /**
     * 获取企业信息注册统计（HTTP接口）
     */
    @GetMapping("/company-statistics")
    @Operation(summary = "获取企业信息注册统计")
    public R<List<Map<String, Object>>> getCompanyStatistics(){
        return R.ok(dashboardManager.getCompanyStatistics());
    }

    /**
     * 获取交易订单统计（HTTP接口）
     */
    @GetMapping("/order-statistics")
    @Operation(summary = "获取交易订单统计")
    public R<Map<String, Object>> getOrderStatistics(){
        return R.ok(dashboardManager.getOrderStatistics());
    }
}